package com.wn.icloud.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Author: liguangcai
 * @Date: 2018/12/19 0019 15:47
 */
public class DateUtil {

    public static DateTimeFormatter dfMinute = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    public static DateTimeFormatter dfMinute2 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
    public static DateTimeFormatter dfTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static DateTimeFormatter dfDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static DateTimeFormatter dfDate2 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    public static DateTimeFormatter dfChinese = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.CHINESE);
    public static DateTimeFormatter dfHHmm = DateTimeFormatter.ofPattern("HH:mm");

    /**
     * 获取分钟的时间戳
     *
     * @param time
     * @return
     */
    public static Long getMinuteTimeStamp(LocalDateTime time) {
        LocalDateTime localDateTime = LocalDateTime.parse(dfMinute.format(time), dfMinute);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * LocalDate 转时间戳
     *
     * @param localDate
     * @return
     */
    public static Long getMinuteTimeStamp(LocalDate localDate) {
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(0, 0));
        return DateUtil.getMinuteTimeStamp(localDateTime);
    }

    /**
     * 转Instant
     *
     * @param localDate
     * @return
     */
    public static Instant toInstant(LocalDate localDate) {
        LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(0, 0));
        return localDateTime.toInstant(ZoneOffset.of("+8"));
    }

    /**
     * 转Instant
     *
     * @param localDateTime
     * @return
     */
    public static Instant toInstant(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8"));
    }

    /**
     * localDateTime 转时间戳
     *
     * @param localDateTime
     * @return
     */
    public static Long toTimeStamp(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 获取两个时间戳之内的分钟数，（向上取）
     *
     * @param time1 较小时间戳
     * @param time2 较大时间戳
     * @return
     */
    public static int getMinutes(Long time1, Long time2) {
        Double slice = Math.ceil((time2 - time1) / 60000);
        return slice.intValue();
    }

    /**
     * 获取两个时间戳之内的分钟数，（向上取）
     * @param startTime 较小时间戳
     * @param endTime 较大时间戳
     * @return
     */
    public static int getMinutes(Instant startTime, Instant endTime){
        return (int) Duration.between(startTime,endTime).toMinutes();
    }

    /**
     * 根据时间戳获取分钟时间戳
     *
     * @param timeStamp
     * @return
     */
    public static Long getMinuteTimeStamp(Long timeStamp) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp), ZoneOffset.of("+8"));
        return getMinuteTimeStamp(localDateTime);
    }

    /**
     * 将时间字符串转化分钟时间戳
     *
     * @param dateStr
     * @return
     */
    public static Long parseStrToMinute(String dateStr) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dfMinute);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * 将时间字符串转为LocalDateTime
     *
     * @param dateStr
     * @return
     */
    public static LocalDateTime parseStrToMinute2(String dateStr) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dfMinute);
        return localDateTime;
    }

    /**
     * 转为LocalDateTime
     *
     * @param instant
     * @return
     */
    public static LocalDateTime toLocalDateTime(Instant instant) {
        return LocalDateTime.ofInstant(instant, ZoneOffset.of("+8"));
    }

    public static LocalDateTime toLocalDatatime(Instant instant) {
        return LocalDateTime.ofInstant(instant, ZoneOffset.of("+0"));
    }

    /**
     * 根据时间戳转化为字符串
     *
     * @param timeStamp
     * @return
     */
    public static String getMinuteStr(Long timeStamp) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp), ZoneOffset.of("+8"));
        return localDateTime.format(dfMinute);
    }

    /**
     * LocalDateTime转化为分钟时间
     *
     * @param localDateTime
     * @return
     */
    public static String getMinuteStr(LocalDateTime localDateTime) {
        return localDateTime.format(dfMinute);
    }

    public static String getMinuteStr2(LocalDateTime localDateTime) {
        return localDateTime.format(dfMinute2);
    }

    public static String getMinuteStr3(Instant instant){
        LocalDateTime time = toLocalDateTime(instant);
        return getMinuteStr(time);
    };

    // 时间转化为字符串
    public static  String getTimeStr(LocalTime time){
        return time.format(dfHHmm);
    }

    /**
     * 时间戳转化为LocalDateTime
     *
     * @param timeStamp
     * @return
     */
    public static LocalDateTime parseToLocalDateTime(Long timeStamp) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(timeStamp), ZoneOffset.of("+8"));
    }

    /**
     * 时间戳转化为LocalDate
     *
     * @param timeStamp
     * @return
     */
    public static LocalDate parseToLocalDate(Long timeStamp) {
        return parseToLocalDateTime(timeStamp).toLocalDate();
    }

    /**
     * 时间戳转化为localDate
     * @param instant
     * @return
     */
    public static LocalDate parseToLocalDate(Instant instant){return parseToLocalDate(instant.toEpochMilli());}

    /**
     * 日期字符串格式化为LocalDate
     *
     * @param dateStr
     * @return
     */
    public static LocalDate parseToDate(String dateStr, DateTimeFormatter df) {
        return LocalDate.parse(dateStr, df);
    }

    public static LocalDateTime parseToDateTime(String dateStr, DateTimeFormatter df) {
        return LocalDateTime.parse(dateStr, df);
    }

    public static Instant toInstant(String dateStr, DateTimeFormatter df) {
        return parseToDateTime(dateStr, df).toInstant(ZoneOffset.of("+8"));
    }

    public static void main(String[] args) {
//        String datestr = "2018-12-20";
//        System.err.println(LocalDate.parse(datestr).getDayOfWeek().getValue());
//        String time1 = "6:0";
//        String[] time1s = time1.split(":");
//        LocalTime localTime = LocalTime.of(Integer.valueOf(time1s[0]), Integer.valueOf(time1s[1]));
//        System.err.println(localTime);
//        Double a = 6.0;
//        String[] aa = String.valueOf(a).split("\\.");
//        System.err.println(aa.length);
//        System.err.println(LocalDate.now());
//        Instant instant = Instant.now();
//        System.err.println(JSON.toJSONString(DateUtil.toTimeStamp(LocalDateTime.of(2018, 12, 24, 17, 02))));
        System.err.println(LocalDateTime.now().format(DateUtil.dfTime));
    }

    /*
    获取指定月份的天数
    参数格式："yyyy-MM-dd"
     */
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /*
    根据年月获取当前月的每一天日期
     */
    public static List<String> getDayByMonth(String yearParamStr, String monthParamStr){
        int yearParam = Integer.parseInt(yearParamStr);
        int monthParam = Integer.parseInt(monthParamStr);
        List list = new ArrayList();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(yearParam,monthParam - 1,1);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate=null;
            if(month<10&&i<10){
                aDate = String.valueOf(year)+"-0"+month+"-0"+i;
            }
            if(month<10&&i>=10){
                aDate = String.valueOf(year)+"-0"+month+"-"+i;
            }
            if(month>=10&&i<10){
                aDate = String.valueOf(year)+"-"+month+"-0"+i;
            }
            if(month>=10&&i>=10){
                aDate = String.valueOf(year)+"-"+month+"-"+i;
            }

            list.add(aDate);
        }
        return list;
    }
}
